﻿using System;
using Flurl.Util;
using Microsoft.Extensions.Caching.Memory;
using NewLife.Caching;
using System.Security.Claims;
using Furion;
using Admin.Net.InformationPlatform.Entity;
using Admin.Net.InformationPlatform.Service.Cache;
using Furion.FriendlyException;
using SqlSugar;
using SjEntityClass;
using Admin.NET.Core.Service;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinUserInfoBatchGetRequest.Types;
using Admin.NET.Core;

namespace Admin.Net.InformationPlatform.Util
{
    /// <summary>
    /// 通用工具类           //General tool class
    /// </summary>
    public class SjCommonUtil
    {
        /// <summary>
        /// 产生雪花ID
        /// </summary>
        /// <returns></returns>
        public static string GenerateNextID()
        {
            return Yitter.IdGenerator.YitIdHelper.NextId().ToString();
        }
        /// <summary>
        /// 字符流水号转换
        /// </summary>
        /// <param name="SerialNumber">流水号</param>
        /// <param name="Increase">流水号增加数</param>
        /// <returns></returns>
        public static string StringSerialNumberConversion(string SerialNumber, int Increase)
        {
            int newRunningNumber = 0;
            string output = "";
            try
            {
                //转换为数字
                newRunningNumber = Convert.ToInt32(SerialNumber);
            }
            catch (Exception)
            {
                return "";
            }
            //流水号增加
            newRunningNumber += Increase;
            //判定流水号增加以后是否有超过整体长度
            if (SerialNumber.Length < newRunningNumber.ToString().Length)
                return "";
            output = newRunningNumber.ToString();
            for (int i = 0; i < SerialNumber.Length - newRunningNumber.ToString().Length; i++)
            {
                output = "0" + output;
            }
            return output;
        }
        /// <summary>
        /// 字符流水号转换为单号，流水号加1
        /// </summary>
        /// <param name="SerialNumber">流水号</param>
        /// <param name="Increase">流水号增加数</param>
        /// <param name="DocumentType">单别</param>
        /// <returns></returns>
        public static string StringSerialNumberConversion(string SerialNumber, int Increase, string DocumentType)
        {
            return DocumentType + StringSerialNumberConversion(SerialNumber, Increase);
        }
        /// <summary>
        /// 用户Token解析
        /// </summary>
        /// <returns></returns>
        public static TokenUser GetUserTokenInfo()
        {
            TokenUser tokenUser = new TokenUser();
            tokenUser.Ent = App.User?.FindFirstValue("Ent") ?? "";
            tokenUser.Site = App.User?.FindFirstValue("Site") ?? "";
            tokenUser.UserId = App.User?.FindFirstValue("UserName") ?? "";

            return tokenUser;
        }
        /// <summary>
        /// 用户Token解析
        /// </summary>
        /// <returns></returns>
        public static TokenUser GetUserTokenInfo(SqlSugarProvider db, SqlSugarProvider db1)
        {
            TokenUser tokenUser = new TokenUser();
            //获取token信息
            tokenUser.Ent = App.User?.FindFirstValue("Ent") ?? "";
            tokenUser.Site = App.User?.FindFirstValue("Site") ?? "";
            tokenUser.UserId = App.User?.FindFirstValue("account") ?? "";
            tokenUser.Workstation = App.User?.FindFirstValue("Workstation") ?? "";
            tokenUser.ID = App.User?.FindFirstValue("UserId") ?? "";
            tokenUser.RealName = App.User?.FindFirstValue("realName") ?? "";
            tokenUser.OrgId = App.User?.FindFirstValue("OrgId") ?? "";
            tokenUser.Phone = App.User?.FindFirstValue("Phone") ?? "";
            tokenUser.Email = App.User?.FindFirstValue("Email") ?? "";

            //获取对应工作站的成本中心
            string costCenter = db.Queryable<EcaaT>().AS("dsdata.ecaa_t").Where(c => c.Ecaaent == tokenUser.Ent && c.Ecaasite == tokenUser.Site && c.Ecaa001 == tokenUser.Workstation).Select(c => c.Ecaa003).First();
            //判断如果未能获取到工作站对应的成本中心，默认以传输值为准
            if (string.IsNullOrEmpty(costCenter))
            {
                tokenUser.SettlementCenter = App.User?.FindFirstValue("SettlementCenter") ?? "";
            }
            else
            {
                tokenUser.SettlementCenter = costCenter;
            }
            //获取t100中人员信息档
            var ooag = db.Queryable<OoagT>().AS("dsdata.ooag_t").Where(c => c.Ooagent == tokenUser.Ent && c.Ooag001 == tokenUser.UserId).Select(c => new { c.Ooag003 }).First();
            //判断人员是否拥有查看全部数据的角色
            var rolecount = db1.Queryable<SysUserRole>().Where(c => c.UserId.ToString() == tokenUser.ID&&c.RoleId== 13946963669701).Count();//查看用户是否拥有查看全部数据的角色
            if (rolecount > 0)
                tokenUser.RoleList = true;

            //判断是否在t100中存在该人员
            if (ooag == null)
            {
                //如果人员不在T100,则清除企业编号于据点信息
                tokenUser.Ent = "";
                tokenUser.Site = "";
                return tokenUser;
            }
           
            //设置部门信息
            tokenUser.Department = ooag.Ooag003;
            return tokenUser;
        }
        /// <summary>
        /// 获取T100据点相关人员信息资料档
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public static TokenUser GetT100UserTokenInfo(SqlSugarProvider db, SqlSugarProvider db1)
        {
            //获取Token
            TokenUser tokenUser = GetUserTokenInfo(db,db1);
            //判断Token是否存在据点于企业编号，不存在则退回该请求
            if (string.IsNullOrEmpty(tokenUser.Site) || string.IsNullOrEmpty(tokenUser.Ent))
            {
                //报错
                //throw Oops.Oh("未获取到据点信息，不允许操作");
            }
            //判断token是否是最新的信息
            if (!App.GetService<InformationSysCacheService>().JudgeUserToken() && tokenUser.UserId != "002948")
            {
                //throw Oops.Oh("用户登录信息与服务器端不符，请重新登录！");
                //throw Oops.Oh("用户异地登录，请重新登录！");
            }
            return tokenUser;




        }




    }
}
